package camera;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import serial.ScreenObject;

public class CompareImage {
	public static boolean[][] changes(BufferedImage old, BufferedImage current,
			int threshold) {
		int subw = old.getWidth() / 10;
		int subh = old.getHeight() / 10;
		int ix = 0;
		int iy = 0;
		boolean[][] changes = new boolean[10][10];

		for (int y = 0; y < old.getHeight(); y += subh) {
			for (int x = 0; x < old.getWidth(); x += subw) {
				changes[iy][ix] = changed(old, current, x, y, x + subw, y
						+ subh, threshold);
			}
			iy++;
			ix = 0;
		}

		return changes;
	}

	public static boolean changed(BufferedImage old, BufferedImage current,
			int x1, int y1, int x2, int y2, int threshold) {
		if (old == null || current == null) {
			return false;
		}
		int sum = 0;

		for (int y = y1; y < y2; y++) {
			for (int x = x1; x < x2; x++) {
				Color c1 = new Color(old.getRGB(x, y));
				Color c2 = new Color(current.getRGB(x, y));
				int currentThreshold = (Math.abs(c1.getRed() - c2.getRed())
						+ Math.abs(c1.getGreen() - c2.getGreen()) + Math.abs(c1
						.getBlue() - c2.getBlue())) / 3;
				if (currentThreshold > 30) {
					sum++;
					if (sum > threshold) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static boolean changedLED(BufferedImage current, int x1, int y1,
			int x2, int y2, int threshold) {
		int diffr = 0;
		int diffg = 0;
		int diffb = 0;
		int difft = 0;

		for (int y = y1; y < y2; y++) {
			for (int x = x1; x < x2; x++) {
				Color c1 = new Color(current.getRGB(x, y));
				diffr += c1.getRed();
				diffg += c1.getGreen();
				diffb += c1.getBlue();
				difft++;
			}
		}
		diffr = diffr / difft;
		diffg = diffg / difft;
		diffb = diffb / difft;
		// System.out.println("Diffr = " + diffr + "\n diffg = " + diffg +
		// "\n diffb = " + diffb);

		if (diffr > threshold) {
			return true;
		} else {
			return false;
		}
	}

	public static BufferedImage changesInImage(BufferedImage old,
			BufferedImage current, int threshold) {
		return changesInImage(old, current, 0, 0, old.getWidth(),
				old.getHeight(), threshold);
	}

	public static BufferedImage changesInImage(BufferedImage old,
			BufferedImage current, int x1, int y1, int x2, int y2, int threshold) {
		if (old == null || current == null) {
			return new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
		}
		BufferedImage newImg = new BufferedImage(old.getWidth(),
				old.getHeight(), BufferedImage.TYPE_INT_RGB);

		int WHITE = Color.WHITE.getRGB();

		for (int y = y1; y < y2; y++) {
			for (int x = x1; x < x2; x++) {
				Color c1 = new Color(old.getRGB(x, y));
				Color c2 = new Color(current.getRGB(x, y));
				int currentThreshold = (Math.abs(c1.getRed() - c2.getRed())
						+ Math.abs(c1.getGreen() - c2.getGreen()) + Math.abs(c1
						.getBlue() - c2.getBlue())) / 3;
				if (currentThreshold > threshold) {
					newImg.setRGB(x, y, WHITE);
				}
			}
		}

		return newImg;
	}

	public static ScreenObject getBlob(BufferedImage image) {
		ScreenObject screenObject = new ScreenObject();
		int WHITE = Color.WHITE.getRGB();
		int MAXZ = 4;
		for (int x = 0; x < image.getWidth() - MAXZ; x++) {
			for (int y = 0; y < image.getHeight() - MAXZ; y++) {
				boolean b = true;
				fz: for (int z1 = 0; z1 < MAXZ; z1++) {
					for (int z2 = 0; z2 < MAXZ; z2++) {
						if (image.getRGB(x + z1, y + z2) != WHITE) {
							b = false;
							break fz;
						}
					}
				}

				if (b) {
					screenObject.addPosition(x, y);
					screenObject.addPosition(x + MAXZ, y + MAXZ);
				}
			}
		}

		return screenObject;
	}

	public static void main(String[] args) throws IOException {
		BufferedImage old = ImageIO.read(CompareImage.class
				.getResource("/resource/86.png"));
		BufferedImage current = ImageIO.read(CompareImage.class
				.getResource("/resource/127.png"));
		// boolean[][] changes = changes(old, current, 100000);
		// for (int y = 0; y < changes.length; y++) {
		// for (int x = 0; x < changes[y].length; x++) {
		// System.out.print(changes[y][x] + " ");
		// }
		// System.out.println();
		// }

		// MC1
		BufferedImage changesInImage = changesInImage(old, current, 6, 6, 70,
				91, 10);
		ImageIO.write(changesInImage, "png", new File("MC1.png"));

		// LED 1
		System.out.println("led 1" + changedLED(current, 110, 1, 177, 28, 205));

		// MC2
		changesInImage = changesInImage(old, current, 127, 41, 207, 140, 10);
		ImageIO.write(changesInImage, "png", new File("MC2.png"));

		// Motor Passo
		changesInImage = changesInImage(old, current, 230, 0, 308, 58, 10);
		ImageIO.write(changesInImage, "png", new File("MP.png"));
	}
}
